<?php

/**
 * 菜单管理
 */
namespace app\admin\controller\admin;

use app\common\controller\Backstage;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("菜单管理")
 */
class Route extends Backstage
{

    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['listApi','info']
        ]
    ];

    /**
     * @Apidoc\Title("菜单管理列表、添加角色用")
     * @Apidoc\Url("/admin/admin.Route/listApi")
     * @Apidoc\Method("POST")
     */
    public function listApi()
    {
        $param = $this->request->param();
        $route = (new \app\common\model\Route())->treeList();
        $list = make_tree($route);
        if (isset($param['parent_id'])) {//修改时添加时无父级
            array_unshift($list, [
                'id' => 0,
                'checked' => false,
                'parent_id' => 0,
                'title' => '顶级',
                'children' => []
            ]);
        }
        return $this->ajax(1, '获取成功', $list);
    }

    /**
     * @Apidoc\Title("详情")
     * @Apidoc\Url("/admin/admin.Route/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="列表id" )
     */
    public function info()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'id|id' => 'require',
            ]);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        $info = (new \app\common\model\Route())
            ->where(['id' => $post['id']])
            ->find()
            ->toArray();
        $info['parent_ids'] = [$info['parent_id']];
        return $this->ajax(1, '获取成功', $info);
    }


    /**
     * @Apidoc\Title("添加修改")
     * @Apidoc\Url("/admin/admin.Route/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", desc="列表id" )
     */
    public function edit()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'title|权限标题' => 'require',
                'controller|控制器' => 'require'
            ]);
            //修改
            if (isset($post['id'])) {
                (new \app\common\model\Route())
                    ->update($post);
            } else {//新增
                (new \app\common\model\Route())
                    ->save($post);
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        return $this->ajax(1, '操作成功');
    }


    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/admin/admin.Route/del")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="列表id" )
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function del()
    {
        $post = $this->request->param();
        try {
            $this->Validate($post, [
                'id|id' => 'require'
            ]);
            //删除
            (new \app\common\model\Route())->where(['id' => $post['id']])->delete();
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getError());
        }
        return $this->ajax(1, '操作成功');
    }
}
